home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / DOCJET.ZIP / data.z / Blank Line Hook.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-18  |  2.6 KB  |  94 lines

  1. /* Copyright (C) 1997-1998, Tall Tree Software Co.
  2.  *
  3.  * This code is provided AS-IS, with no warranty expressed or implied.
  4.  *  in no way shall Tall Tree Software Co. be held liable for damages
  5.  *  brought about by the use of this software.
  6.  */
  7.  
  8. #include <windows.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <assert.h>
  12.  
  13. #include <Rewriter Hook.h>
  14.  
  15. char *sourceExtensions[] = { "c", "cc", "cpp", "cxx", "h", "hxx", "hpp", "inl",
  16.                  "idl",
  17.                  0 };
  18.  
  19. extern "C" DLLEXPORT bool RewriteSource( const char *sourceFile,
  20.                      const char *tmpFile )
  21. {
  22.     // See if this is a C, C++, IDL, or VB file...
  23.     const char *ext = strrchr( sourceFile, '.' );
  24.     assert( ext != 0 );
  25.     if ( ext == 0 )
  26.       return( false );
  27.     ext += 1;
  28.     char **ep = sourceExtensions;
  29.     while ( *ep != 0 && 0 != stricmp( ext, *ep ) )
  30.       ++ep;
  31.     if ( *ep == 0 )
  32.       return( false ); // not a source code file.
  33.  
  34.  
  35.     FILE *input = fopen( sourceFile, "r" );
  36.     FILE *output = fopen( tmpFile, "w" );
  37.     assert( input != 0 && output != 0 );
  38.     if ( input == 0 || output == 0 ) {
  39.         if ( input != 0 )
  40.           fclose( input );
  41.         if ( output != 0 )
  42.           fclose( output );
  43.         return( FALSE );
  44.     }
  45.  
  46.     // Our plan here is to look for lines that end a /* */ comment or // that
  47.     //   are followed by blank lines.
  48.     char thisLine[1024];
  49.     char nextLine[1024];
  50.     fgets( thisLine, sizeof(thisLine), input );
  51.     for (;;) {
  52.         if ( 0 != strstr( thisLine, "*/" ) &&
  53.              strlen(strstr(thisLine,"*/")+2) ==
  54.               strspn(strstr(thisLine,"*/")+2, " \t") ) {
  55.             if ( 0 == fgets( nextLine, sizeof(nextLine), input ) ) {
  56.                 fputs( thisLine, output );
  57.                 break;
  58.             }
  59.             else if ( strlen( thisLine ) == strspn( thisLine, " \t\n" ) ) {
  60.                 *strstr(thisLine, "*/") = '\0';
  61.                 fputs( thisLine, output );
  62.                 /* Next line is blank, so put in an empty comment. */
  63.                 fputs( "*/\n", output );
  64.                 if ( 0 == fgets( thisLine, sizeof(thisLine), input ) )
  65.                   break;
  66.             }
  67.             else {
  68.                 fputs( thisLine, output );
  69.                 strcpy( thisLine, nextLine );
  70.             }
  71.         }
  72.         else if ( 0 == strncmp( thisLine+strspn(thisLine, " \t"), "//", 2 ) ) {
  73.             fputs( thisLine, output );
  74.             if ( 0 == fgets( thisLine, sizeof(thisLine), input ) )
  75.               break;
  76.             else if ( strlen( thisLine ) == strspn( thisLine, " \t\n" ) ) {
  77.                 /* Next line is blank, so put in an empty comment. */
  78.                 fputs( "//\n", output );
  79.                 if ( 0 == fgets( thisLine, sizeof(thisLine), input ) )
  80.                   break;
  81.             }
  82.         }
  83.         else {
  84.             fputs( thisLine, output );
  85.             if ( 0 == fgets( thisLine, sizeof(thisLine), input ) )
  86.               break;
  87.         }
  88.     }
  89.     fclose( output );
  90.     fclose( input );
  91.     return( TRUE );
  92. }
  93.  
  94.